package com.snake.mylibrary.loader;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snake.mylibrary.net.HttpStatus;

import java.util.HashMap;
import java.util.Map;

	public class ViewEmptyHolder extends LinearLayout implements
		ProgressNotifiable {
	private ProgressView progress;

	private DisplayView display;//empty的view

	private boolean hasNoMoreData;
	
	private static HashMap<HttpStatus, String> DEF_ERR ;

	public ViewEmptyHolder(Context context) {
		this(context, null);
		initViews();
	}

	public ViewEmptyHolder(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initViews();
	}

	private void initViews()
	{
		if(DEF_ERR == null)
		{
			DEF_ERR = new HashMap<>();
			DEF_ERR.put(HttpStatus.HTTP_IO_EXCEPTION, "");
			DEF_ERR.put(HttpStatus.HTTP_NETWORK_NOT_READY,"" );
			DEF_ERR.put(HttpStatus.HTTP_SERVER_EXCEPTION,"");
			DEF_ERR.put(HttpStatus.HTTP_UNKNOWN_EXCEPTION, "");
			DEF_ERR.put(HttpStatus.PROTOCOL_BUSY_NOW, "");
			DEF_ERR.put(HttpStatus.HAS_NO_DATA, "");
			//用户没有登陆
			DEF_ERR.put(HttpStatus.HTTP_OK_NOT_LOGIN,"");
		}

	}
	//开始加载数据，回调
	@Override
	public void onLoadStart(boolean hasFirstPageData) {
		progress.update(hasFirstPageData);
		display.setVisibility(GONE);
		progress.setVisibility(VISIBLE);
		setVisibility(VISIBLE);
	}
	//加载数据完成，回调
	@Override
	public void onLoadStop(boolean hasFirstPageData, Map<HttpStatus, String> txt,
			HttpStatus err) {
		progress.setVisibility(GONE);//加载界面
		display.setVisibility(GONE);//
		setVisibility(GONE);
		if(err == HttpStatus.PROTOCOL_BUSY_NOW)
		{
			return ;
		}
		if(err == HttpStatus.PROTOCOL_NO_MORE_DATA)
		{
			if(hasNoMoreData)
			{
				return ;
			}
			hasNoMoreData = true;
			if(hasFirstPageData)
			{
			}
			
			return ;
		}



		if(err == HttpStatus.HAS_NO_DATA){
			display.setVisibility(VISIBLE);
			display.update(err, txt);
			setVisibility(VISIBLE);
			return;
		}
		if( err == HttpStatus.PROTOCOL_UPDATE_DATA)
		{
			return ;
		}
		if(hasFirstPageData && err == HttpStatus.HTTP_OK)
		{
			return ;
		}
		
		if(!hasFirstPageData)//数据不可用
		{
			//此情况使用了自定义view显示
			if(err==HttpStatus.HTTP_OK_NOT_LOGIN){
				display.setVisibility(VISIBLE);
				display.update(err, txt);
				setVisibility(VISIBLE);
				return;
			}
			display.setShowCustom(false);
			display.setVisibility(VISIBLE);
			display.update(err, txt);
			setVisibility(VISIBLE);
		}else
		{
			setVisibility(GONE);
			if(err != HttpStatus.HTTP_OK)
			{
			}
		}
	}
	
	public void setProgressText(CharSequence text)
	{
		progress.textView.setText(text);
	}
	
	public void setReloadListener(OnClickListener reload)
	{
		display.setReloadListener(reload);
	}

	class ProgressView {
		private TextView textView;
		private View content;
		public ProgressView(View content) {
			this.content = content;
		}

		public void update(boolean dataExist) {
			content.setVisibility(VISIBLE);
			if (dataExist) {
				getLayoutParams().height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
				textView.setVisibility(View.GONE);
			} else {
				textView.setVisibility(View.VISIBLE);
				getLayoutParams().height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
			}
		}
		
		public void setVisibility(int visibility)
		{
			content.setVisibility(visibility);
		}
	}
	

	
	private String getErrString(HttpStatus err, Map<HttpStatus, String> text)
	{
		String t = null;
		HashMap<HttpStatus, String> txt = new HashMap<>(DEF_ERR);
		if(text != null)
		{
			txt.putAll(text);
		}
		if(text != null && text.get(err)!=null)
		{
			t = text.get(err);
		}else
		{
			t = DEF_ERR.get(err);
			if(TextUtils.isEmpty(t))
			{
			}
		}
		return t;
	}
	
	class DisplayView  {
		private View content;
		private OnClickListener reload;
		private boolean showCustom = false;
		
		public DisplayView(View content) {
			this.content = content;

		}

		public void update(HttpStatus err, Map<HttpStatus, String> text)
		{

		}
		
		public void setReloadListener(OnClickListener reload)
		{
			this.reload = reload;
		}
		
		public void setVisibility(int visibility)
		{
			content.setVisibility(visibility);
			if(showCustom){

			}else{

			}
		}

		public void setShowCustom(boolean b){
			showCustom = b;
		}

	}



}
